package com.open.party.cloud.view.home.eJia.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.open.party.cloud.R;
import com.open.party.cloud.model.FreePhotosBean;
import com.open.party.cloud.model.PartyLifeBean;
import com.sinothk.android.utils.XUtils;

import java.util.ArrayList;
import java.util.List;

import com.sinothk.android.utils.inters.OnSuperListener;

public class PartyLifeListAdapter extends RecyclerView.Adapter<PartyLifeListAdapter.ViewHolder> {

    // 展示数据
    private ArrayList<PartyLifeBean> mData;
    private Context mContext;
    private OnSuperListener<PartyLifeBean> superListener;

    public PartyLifeListAdapter(Context mContext) {
        this.mContext = mContext;
        this.mData = new ArrayList<>();
    }

    public void setOnSuperListener(OnSuperListener<PartyLifeBean> superListener) {
        this.superListener = superListener;
    }

    public void setData(List<PartyLifeBean> data) {
        if (mData == null) {
            mData = new ArrayList<>();
        } else {
            mData.clear();
        }
        if (data == null) {
            data = new ArrayList<>();
        }
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    public void updateData(List<PartyLifeBean> data) {
        this.mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.party_life_list_item, viewGroup, false);
        return new ViewHolder(view);
    }

    @SuppressLint("SetTextI18n")
    @Override
    public void onBindViewHolder(ViewHolder holder, final int position) {
        // 绑定数据
        final PartyLifeBean itemBean = mData.get(position);

        holder.titleTv.setText(XUtils.string().getNotNullValue(itemBean.getName()));
        holder.orgNameTv.setText("党组织：" + XUtils.string().getNotNullValue(itemBean.getOrgName()));
        holder.leaderTv.setText("负责人：" + XUtils.string().getNotNullValue(itemBean.getDutyPerson()));
        holder.addressTv.setText("活动地点：" + XUtils.string().getNotNullValue(itemBean.getAddr()));

        String timeStartStr = XUtils.date().getDateStrByDate(itemBean.getStartTime(), "yyyy-MM-dd HH:mm");
        String timeEndStr = XUtils.date().getDateStrByDate(itemBean.getEndTime(), "yyyy-MM-dd HH:mm");
        holder.dateTv.setText("活动时间：" + timeStartStr + " - " + timeEndStr);

        // 分割线
        if (position != getItemCount() - 1) {
            holder.lineView.setVisibility(View.GONE);
        } else {
            holder.lineView.setVisibility(View.VISIBLE);
        }

        if (superListener != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    superListener.onClick(position, itemBean, "");
                }
            });
        }
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        LinearLayout rootView;
        TextView lineView, lineView0, titleTv, orgNameTv, leaderTv, dateTv, addressTv;

        ViewHolder(View view) {
            super(view);
            rootView = view.findViewById(R.id.rootView);

            lineView = view.findViewById(R.id.lineView);
            lineView0 = view.findViewById(R.id.lineView0);

            titleTv = view.findViewById(R.id.titleTv);
            orgNameTv = view.findViewById(R.id.orgNameTv);
            leaderTv = view.findViewById(R.id.leaderTv);
            dateTv = view.findViewById(R.id.dateTv);
            addressTv = view.findViewById(R.id.addressTv);
        }
    }
}